﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using FeedAgregator;
using DDNY.WebPortals.Common;

public partial class ViewPage : System.Web.UI.Page
{
    public int FeedItemId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            foreach (string key in Request.QueryString)
            {
                switch (key.ToLower())
                {

                    case "id":
                        FeedItemId = int.Parse(Request.QueryString[key]);
                        break;
                    default:
                        break;
                }

            }

            if (Request.QueryString["title"] == null)
            {
                using (FeedSignalDataContext ctx = new FeedSignalDataContext())
                {
                    FeedItem item = (from feedItems in ctx.FeedItems
                                     where feedItems.ID == FeedItemId
                                     select feedItems).SingleOrDefault();
                    string urlTitle = UrlHelper.GenerateGoodTitleQueryString(item.Title);

                    string redirectUrl = Request.Url.AbsoluteUri + "&title=" + urlTitle;
                    // new line here
                    HttpContext.Current.Response.Clear();
                    //
                    HttpContext.Current.Response.Status = "301 Moved Permanently";
                    HttpContext.Current.Response.AddHeader("Location", redirectUrl);
                    Response.End();

                }
            }


            using (FeedSignalDataContext ctx = new FeedSignalDataContext())
            {
                FeedItem item = (from feedItems in ctx.FeedItems
                                 where feedItems.ID == FeedItemId
                                 select feedItems).SingleOrDefault();

                Iframemain.Attributes.Add("onload", "resize()");
                Iframemain.Attributes.Add("src", item.ItemUrl);
                ReadHistory historyToAdd = new ReadHistory();
                historyToAdd.FeedItemID = item.ID;
                historyToAdd.DateReaded = DateTime.Now;
                ctx.ReadHistories.InsertOnSubmit(historyToAdd);
                ctx.SubmitChanges();


                YouAreViewingLink.Text = HttpUtility.UrlDecode(item.ItemUrl);
                YouAreViewingLink.NavigateUrl = "~/Redirect.aspx?itemid=" + item.ID.ToString();
                LinkWithTitle.Text = item.Title;
                LinkWithTitle.NavigateUrl = "~/Redirect.aspx?itemid=" + item.ID.ToString();
                RemoveFrameLink.NavigateUrl = "~/Redirect.aspx?itemid=" + item.ID.ToString();
                this.Title = item.Title + " - " + item.Feed.FeedName + " - " + item.PubDate.ToString("dd.MM.yyyy") + " - " + item.Feed.FeedCategory.CategoryName;
                if (Request.UrlReferrer != null && Request.UrlReferrer.Authority == Request.Url.Authority)
                {
                    BackLink.NavigateUrl = Request.UrlReferrer.AbsoluteUri;
                }

                //Add to Reading History

                //BackLink.Attributes.Add("onclick", "window.history.go(-1);");
            }


        }


    }

   
}
